package com.panopset.io;

import java.io.File;

/**
 * Process all files in a directory, and it's subdirectories.
 *
 * @author Karl Dinwiddie
 *
 */
public abstract class DirectoryProcessor {

    private final File d;

    protected abstract void process(File f);

    protected boolean isThisFileToBeProcessed(File f) {
        return true;
    }

    /**
     * Process a directory and its subdirectories.
     * @param dir Top directory to process
     */
    public DirectoryProcessor(File dir) {
        this.d = dir;
    }

    /**
     * Do the traversal.
     */
    public void exec() {
        traverse(this.d);
    }

    private void traverse(File file) {
        if (file.isFile()) {
            if (isThisFileToBeProcessed(file)) {
                process(file);
            }
        } else {
            for (File f : file.listFiles()) {
                traverse(f);
            }
        }
    }

}
